package com.example.controllers;

import com.example.dao.UsersMapper;
import com.example.pojo.Users;
import com.example.service.LoginAdmin;
import com.example.service.LoginBoss;
import com.example.service.LoginUser;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UsersMapper usersMapper;

    private Users user;

    @Autowired
    LoginUser loginUser;

    @Autowired
    LoginBoss loginBoss;

    @Autowired
    LoginAdmin loginAdmin;

    @RequestMapping("/login")
    public String login(HttpSession session, String username, String password){
        boolean isLogin = loginUser.login(username,password);
        if(isLogin==true){
            session.setAttribute("username",username);
            session.setAttribute("password",password);
            return "forward:loginAdmin";
        }
        return "log.html";

    }

    @RequestMapping("/loginAdmin")
    public String loginA(HttpSession session){

        String name = (String)session.getAttribute("username");
        String psw = (String)session.getAttribute("password");
        boolean isLogined = loginAdmin.loginAdmin(name,psw);
        if(isLogined==true){
            return "forward:loginBoss";
        }

        return "login.html";

    }

    @RequestMapping("/loginBoss")
    public String loginB(HttpSession session){

        String name = (String) session.getAttribute("username");
        String psd = (String ) session.getAttribute("psd");

        boolean isLogined = loginBoss.loginBoss(name,psd);

        if(isLogined == true){
            return "forward:loginBoss.html";
        }
        return "redirect:loginAdmin.html";
    }

    /*unum
            username
    password
            realname
    sex
            age
    iden
            tel
    email
            province
    city
            district
    details
            latitude
    longtitude*/

    public ResponseEntity<byte[]> test(HttpServletResponse response) throws IOException {

        XSSFWorkbook workbook = new XSSFWorkbook();

        XSSFSheet sheet = workbook.createSheet("testsheet");

        List<Users> list = usersMapper.selectAll();

        XSSFRow headrow = sheet.createRow(0);

        headrow.createCell(0).setCellValue("");


        for (int i = 0; i < list.size(); i++) {
            Users user = list.get(i);
            XSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue("");


        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        workbook.write(baos);

        byte[] re = baos.toByteArray();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment","user.xlsx");
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(re, headers, HttpStatus.CREATED);

        return responseEntity;
    }

    public String test(Map<String, Users> map){
        Users user = usersMapper.selectByPrimaryKey(2);
        map.put("user", user);
        return "userinfo";
    }


}
